﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace UpdaterForm
{
    public class Settings
    {
        private static Configuration _instance;
        private const string ConfFile = @".\Conf.xml";

        private Settings()
        {
        }

        public static Configuration Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = LoadConf();
                }
                return _instance;
            }
        }

        private static Configuration LoadConf()
        {
            //TODO: add exception handling for file not found ir invalid directory
            var serializer = new XmlSerializer(typeof(Configuration));
            using (var reader = new StreamReader(ConfFile))
            {
                return (Configuration)serializer.Deserialize(reader);
            }
        }

        public static void SaveConf()
        {
            //TODO: add exception handling for file not found ir invalid directory
            if (_instance != null){
                var serializer = new XmlSerializer(typeof(Configuration));
                using (var writer = new StreamWriter(ConfFile)){
                    serializer.Serialize(writer, _instance);
                }
            }
        }

    }

}
